/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package cc.aileron.commons.utility;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * ジェネリクスなクラスユーティリティー
 * @author Aileron
 *
 */
public class GenericClassParameterUtils
{
    /**
     * クラスの型パラメータを取得
     * @param object
     * @return
     */
    public static Type[] getSuperClassParameters( final Object object )
    {
        if(object==null)
        {
            throw new NullPointerException();
        }
        return getSuperClassParameters( object.getClass() );
    }
    
    /**
     * クラスの型パラメータを取得
     * @param classObject
     * @return
     */
    public static Type[] getSuperClassParameters( final Class<?> classObject )
    {
        if(classObject == null)
        {
            throw new NullPointerException();
        }
        final Type type = classObject.getGenericSuperclass();
        if(! (type instanceof ParameterizedType))
        {
            throw new NullPointerException();
        }
        final ParameterizedType pType = (ParameterizedType) type;
        return pType.getActualTypeArguments();
    }
    
    /**
     * 指定したインターフェスの型パラメータの取得
     * @param object
     * @param interfaceClass
     * @return
     */
    public static Type[] getInterfaceClassParameters( final Object object, final Class<?> interfaceClass)
    {
        return getInterfaceClassParameters(object.getClass(), interfaceClass);
    }
    
    /**
     * 指定したインターフェスの型パラメータの取得
     * @param classObject
     * @param interfaceClass
     * @return
     */
    public static Type[] getInterfaceClassParameters( final Class<?> classObject, final Class<?> interfaceClass)
    {
        if( classObject==null || interfaceClass==null )
        {
            throw new NullPointerException();
        }
        for(final Type t : classObject.getGenericInterfaces())
        {
            if(!(t instanceof ParameterizedType))
            {
                continue;
            }
            final ParameterizedType type = (ParameterizedType) t;
            if(interfaceClass.equals(type.getRawType()))
            {
                return type.getActualTypeArguments();
            }
        }
        throw new NullPointerException();
    }
    
    /**
     * type から クラスの取得
     * @param <T>
     * @param type
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> castTypeToClass( final Type type )
    {
        return (Class<T>) type;
    }
}